#pragma once

using namespace std;

#include "VarTable.h"

VarTable::VarTable()
{
	count = 0;
};

int VarTable::getSize()
{		
	return count;
};

void VarTable::updateLastMod(INDEX varName,INDEX lastMod)
{
	unordered_map<INDEX,INDEX>::const_iterator got = varTableLastModified.find(varName);

	if(got == varTableLastModified.end())
	{
		varTableLastModified.insert(std::make_pair<INDEX,INDEX>(varName,lastMod)); 		
	}
	else
	{
		varTableLastModified.at(varName) = lastMod;
	}
};

INDEX VarTable::getLastMod(INDEX varName)
{
	unordered_map<INDEX,INDEX>::const_iterator got = varTableLastModified.find(varName);

	if(got == varTableLastModified.end())
	{
		return -1;
	}
	else
	{
		return varTableLastModified.at(varName);
	}
};

INDEX VarTable::insertVar(NAME varName)
{
	INDEX ret = getVarIndex(varName);
	if(ret==-1)
	{
		++count;
		varTable.insert(std::make_pair<INDEX,NAME>(count,varName)); 
		return count;
	}
	else
	{
		return ret;
	}
};

NAME VarTable::getVarName(INDEX ind)
{
	unordered_map<INDEX,NAME>::const_iterator got = varTable.find(ind);

	if(got == varTable.end())
	{
		return NULL;
	}
	else
	{
		return got->second;
	}
};

INDEX VarTable::getVarIndex(NAME varName)
{
	if(varTable.empty())
	{
		return -1;
	}
	for(int i = 1;i<=getSize();i++)
	{
		if(varTable.at(i)==varName) 
		{
			return i;
		}
	}
	return -1;
};

void VarTable::printVarTable()
{
	cout << "Printing Var Table" << endl;
	for ( auto it = varTable.begin(); it != varTable.end(); ++it )
	{
		cout << it->first << " : " << it->second << endl;
	}
};